﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class RewardsMediator extends RenderListMediator implements ITicking {

		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var tutorialModel:TutorialModel;
		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var rewardsDisplay:RewardsDisplay;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var gameStateModel:GameStateModel;
        private var _needsInventoryCheck:Dictionary;
        private var _rewardItemVOs:Array;

        public function RewardsMediator(){
            this._rewardItemVOs = [];
            super();
        }
        override public function onRegister():void{
            if (this.userModel.visitingFriendID == this.userModel.mySocialID){
                eventMap.mapListener(eventDispatcher, EventConstants.SHOW_REWARD_ICONS, this.showRewardIcons);
                eventMap.mapListener(eventDispatcher, EventConstants.FADE_ITEMS, this.onFadeItem);
                eventMap.mapListener(this.rewardsDisplay, EventConstants.REWARD_ICON_CLICKED, this.rewardIcon_click);
                this._needsInventoryCheck = new Dictionary();
                this._needsInventoryCheck[GameConstants.REWARD_BATTERY] = true;
                this._needsInventoryCheck[GameConstants.REWARD_COFFEE] = true;
                this._needsInventoryCheck[GameConstants.REWARD_COFFEE_PACK] = true;
                this.rewardsDisplay.addEventListener(Event.ENTER_FRAME, this.addTicking);
            };
        }
        private function addTicking(_arg1:Event):void{
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
            if (this.rewardsDisplay){
                this.rewardsDisplay.removeEventListener(Event.ENTER_FRAME, this.addTicking);
            };
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            var _local3:RewardItemVO;
            var _local4:String;
            if (this.areConditionsValidForTick()){
                return;
            };
            for each (_local3 in this._rewardItemVOs) {
                _local3.selfDestructIn--;
                if (((!(_local3.alreadyCollected)) && ((_local3.selfDestructIn <= 0)))){
                    _local4 = ((this.worldModel.getRoomIdsOfType(GameConstants.TYPE_BATTERY_ROOM)[0]) || (null));
                    if (_local4 != _local3.roomId){
                        if (this.handleClickedRewardItem(_local3)){
                            return;
                        };
                    };
                };
            };
        }
        private function areConditionsValidForTick():Boolean{
            return (!(((((this.userModel.isVisitFriendsMode()) || (this.gameStateModel.isFacadeVisible))) || ((this.userModel.level > 1)))));
        }
        private function rewardIcon_click(_arg1:InfoEvent):void{
            var _local2:RewardItemVO = this.getClickedRewardItemVOByID(_arg1.id);
            ((_local2) && (this.handleClickedRewardItem(_local2, true)));
        }
        private function showRewardIcons(_arg1:InfoEvent):void{
            var _local12:Boolean;
            var _local13:RewardItemVO;
            var _local14:Boolean;
            var _local15:String;
            var _local2:String = _arg1.id;
            var _local3:IRoom = IRoom(this.worldModel.getRoomById(_local2));
            var _local4:Slot = this.roomSlotsModel.getSlotById(_local3.slotId);
            var _local5:Point = _local4.position.clone();
            var _local6:Boolean = (((this.tutorialModel.state > TutorialModel.LEVEL1_BATTERY_TO_TREATMENT)) && ((this.tutorialModel.state < TutorialModel.LEVEL1_BOOST_PATIENT_FINISH)));
            var _local7 = (_local3.type == GameConstants.TYPE_BATTERY_ROOM);
            var _local8:Boolean = this.tutorialModel.isInTutorialMode;
            var _local9:Point = new Point((_local5.x + _local3.rewardStartPos.x), ((_local5.y + GameConstants.ROOM_HEIGHT) - _local3.rewardStartPos.y));
            var _local10:int = (_local3.rewardLandingXStart + _local5.x);
            var _local11:int = (_local3.rewardLandingXEnd + _local5.x);
            if (_local8){
                _local10 = (_local10 + 250);
                _local11 = _local10;
            };
            this.rewardsDisplay.setRewardPosition(_local9, _local10, _local11);
            this.rewardsDisplay.setBackgroundAnimationPosition(_local5);
            if ((((_local3.rewardItems.length > 0)) && (((!(_local6)) || (_local7))))){
                _local12 = (_arg1.info as Boolean);
                for each (_local13 in _local3.rewardItems) {
                    _local14 = (Arrays.findAllByAttribute(this._rewardItemVOs, "id", _local13.id).length > 0);
                    if (!_local14){
                        _local13.roomId = _local3.id;
                        _local15 = ("+ " + this.languageModel.getTextByKey(this.getTextKey(_local13.itemType), {amount:_local13.amount}));
                        this.rewardsDisplay.addRewardItem(_local13.id, _local13.itemType, _local15, _local12);
                        this._rewardItemVOs.push(_local13);
                    };
                };
            };
        }
        protected function getTextKey(_arg1:String):String{
            var _local2:String = _arg1.replace("Reward", "");
            _local2 = _local2.toLowerCase();
            switch (_local2){
                case "coin":
                case "coinstack":
                case "gold":
                    _local2 = "coins";
                    break;
                case "xp":
                case "xppack":
                    _local2 = "xp";
                    break;
            };
            _local2 = ("global_amount_" + _local2);
            return (_local2);
        }
        private function handleClickedRewardItem(_arg1:RewardItemVO, _arg2:Boolean=false):Boolean{
            var _local3:IRoom = IRoom(this.worldModel.getRoomById(_arg1.roomId));
            var _local4:String = _arg1.itemType;
            if (((this._needsInventoryCheck[_local4]) && (this.userModel.isFull(_local4)))){
                ((_arg2) && (dispatch(new InfoEvent(EventConstants.SET_SUN_SPEECH_BUBBLE, "sun_message_inventory_full"))));
                return (false);
            };
            if (!_arg1.alreadyCollected){
                this.createRewardAnimation(_arg1);
                this.checkRewardItemVOArraySize();
                if (_arg1.isCollectionItem){
                    dispatch(new InfoEvent(EventConstants.COLLECT_COLLECTION_REWARD_ITEM, _arg1.id, _local3));
                } else {
                    dispatch(new InfoEvent(EventConstants.COLLECT_REWARD_ITEM, _arg1.id, _local3));
                };
                _arg1.alreadyCollected = true;
                return (true);
            };
            return (false);
        }
        private function createRewardAnimation(_arg1:RewardItemVO):void{
            var _local2:MovieClip = MovieClip(this.rewardsDisplay.getChildByName(_arg1.id));
            var _local3:Object = new Object();
            _local3[EventConstants.ITEM_TO_COLLECT] = _local2;
            _local3[EventConstants.ITEM_TYPE] = _arg1.itemType;
            dispatch(new InfoEvent(EventConstants.CREATE_REWARD_ANIMATION_FROM_HOSPITAL, null, _local3));
            this.rewardsDisplay.collectRewardItem(_arg1.id);
            ((_arg1.isCollectionItem) && (this.updateCollectionPopups(_arg1)));
        }
        private function getClickedRewardItemVOByID(_arg1:String):RewardItemVO{
            var _local2:RewardItemVO;
            for each (_local2 in this._rewardItemVOs) {
                if (_local2.id == _arg1){
                    return (_local2);
                };
            };
            return (null);
        }
        private function checkRewardItemVOArraySize():void{
            if (this._rewardItemVOs.length > 200){
                this._rewardItemVOs.splice(0, 50);
            };
        }
        private function onFadeItem(_arg1:InfoEvent=null):void{
            this.rewardsDisplay.alpha = _arg1.info.alpha;
            this.rewardsDisplay.mouseEnabled = (this.rewardsDisplay.mouseChildren = _arg1.info.enabled);
            if (_arg1.info.enabled){
                dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
            } else {
                dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
            };
        }
        override public function onRemove():void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
            super.onRemove();
        }
        private function updateCollectionPopups(_arg1:RewardItemVO):void{
            dispatch(new InfoEvent(EventConstants.UPDATE_COLLECTION_POPUPS, null, _arg1));
        }

    }
}//package net.wooga.pets.views 
